﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using MediaServer.Utilities;
using MediaServer.Client;

namespace MediaServer
{
    public partial class MediaServerManager : Form, ILogger
    {
        private SimpleMediaServer device;

        private VirtualDirectory music = new VirtualDirectory("Music", "0", "1");
        private VirtualDirectory photos = new VirtualDirectory("Photos", "0", "2");
        private VirtualDirectory videos = new VirtualDirectory("Videos", "0", "3");

        public MediaServerManager()
        {
            InitializeComponent();
            device = new SimpleMediaServer(this);
            device.GetRoot().AddChild(music);
            device.GetRoot().AddChild(photos);
            device.GetRoot().AddChild(videos);

            this.musicList.DataSource = music.GetChildren();
            this.photosList.DataSource = photos.GetChildren();
            this.videosList.DataSource = videos.GetChildren();
        }

        public void log(string message)
        {
            if (InvokeRequired)
            {
                this.Invoke((MethodInvoker)delegate
                {
                    appendLog(message);
                });
            }
            else
            {
                appendLog(message);
            }
        }

        private void appendLog(string message)
        {
            if (logTimeCheckbox.Checked)
            {
                message = DateTime.Now.ToString("HH:mm:ss ") + message;
            }
            logBox.AppendText(message + "\n");
        }

        private void startButton_Click(object sender, EventArgs e)
        {
            if (device == null)
            {
                device = new SimpleMediaServer(this);
            }
            log("Starting UPnP Media Server...");
            device.Start();

            stopButton.Enabled = true;
            startButton.Enabled = false;
        }

        private void stopButton_Click(object sender, EventArgs e)
        {
            log("Stopping UPnP Media Server...");
            if (device != null)
            {
                device.Stop();
            }

            stopButton.Enabled = false;
            startButton.Enabled = true;
        }

        private void addMusicButton_Click(object sender, EventArgs e)
        {
            DialogResult result = folderBrowserDialog.ShowDialog();
            if (result == DialogResult.OK)
            {
                string path = folderBrowserDialog.SelectedPath;
                music.AddChild(new SharedDirectory(path, "1"));
                musicList.DataSource = null;
                musicList.DataSource = music.GetChildren();
            }
        }

        private void removeMusicButton_Click(object sender, EventArgs e)
        {
            music.RemoveChild((SharedDirectory)musicList.SelectedValue);
            musicList.DataSource = null;
            musicList.DataSource = music.GetChildren();
        }

        private void addPhotosButton_Click(object sender, EventArgs e)
        {
            DialogResult result = folderBrowserDialog.ShowDialog();
            if (result == DialogResult.OK)
            {
                string path = folderBrowserDialog.SelectedPath;
                photos.AddChild(new SharedDirectory(path, "2"));
                photosList.DataSource = null;
                photosList.DataSource = photos.GetChildren();
            }
        }

        private void removePhotosButton_Click(object sender, EventArgs e)
        {
            photos.RemoveChild((SharedDirectory)photosList.SelectedValue);
            photosList.DataSource = null;
            photosList.DataSource = photos.GetChildren();
        }

        private void addVideosButton_Click(object sender, EventArgs e)
        {
            DialogResult result = folderBrowserDialog.ShowDialog();
            if (result == DialogResult.OK)
            {
                string path = folderBrowserDialog.SelectedPath;
                videos.AddChild(new SharedDirectory(path, "2"));
                videosList.DataSource = null;
                videosList.DataSource = videos.GetChildren();
            }
        }

        private void removeVideosButton_Click(object sender, EventArgs e)
        {
            videos.RemoveChild((SharedDirectory)videosList.SelectedValue);
            videosList.DataSource = null;
            videosList.DataSource = videos.GetChildren();
        }

        private void clientButton_Click(object sender, EventArgs e)
        {
            // Allow multiple clients to be created
            new MediaClientManager().Show();
        }

        private void logClearButton_Click(object sender, EventArgs e)
        {
            logBox.Clear();
        }
    }
}
